//
//  DetailedViewController.m
//  片刻
//
//  Created by lanou3g on 15/6/5.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "DetailedViewController.h"
#import "MaocHeader.h"
#import "DetaildTableViewCell.h"
@interface DetailedViewController ()
@property (nonatomic,retain) UIScrollView *mainScroll; //主的scroll
@property (nonatomic,retain) UITableView *liftTabel;    //左边的Tabel；
@property (nonatomic,retain) UITableView *rightTabel; //右边的tabel；
@property (nonatomic,retain) NSMutableArray *readLiftArr; //左边数组
@property (nonatomic,retain) NSMutableArray *readRightArr; //右边数组
@property (nonatomic,assign) BOOL loadin; //判断yes no
@end

@implementation DetailedViewController
-(void)dealloc
{
    self.mainScroll = nil;
    self.liftTabel = nil;
    self.rightTabel = nil;
    self.readLiftArr = nil;
    self.readRightArr = nil;
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.readLiftArr = [NSMutableArray array];
    self.readRightArr = [NSMutableArray array];
    self.view.backgroundColor= [UIColor whiteColor];
    _loadin = NO;
    [self createData];
    [self createScrollView];
    [self createLiftTabelView];
}

-(void)createScrollView
{
    _mainScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH , SCREEN_HiGHT - 70)];
    _mainScroll.contentSize = CGSizeMake(SCREEN_WIDTH * 2, 0); //scroll内容大小
//    _mainScroll.scrollsToTop = YES; //点导航条会回来
    _mainScroll.pagingEnabled= YES; //是否整屏滑动
    _mainScroll.bounces  =NO;  //回弹效果
    _mainScroll.delegate = self;
   
    [self.view addSubview:_mainScroll];
    [_mainScroll release];
    
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{

    if (scrollView.contentOffset.x >= SCREEN_WIDTH && _loadin == NO) {
                [self createData];
                [self createRightTabelView];
            }
}

-(void)createLiftTabelView
{
    _liftTabel = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HiGHT-70)];
    _liftTabel.delegate = self;
    _liftTabel.dataSource = self;
    [_liftTabel registerClass:[DetaildTableViewCell class] forCellReuseIdentifier:@"lift"];
    [self.mainScroll addSubview:_liftTabel];
    
    [_liftTabel release];
}

-(void)createRightTabelView
{
    _rightTabel = [[UITableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HiGHT-70)];
    _rightTabel.delegate = self;
    _rightTabel.dataSource = self;
    [_rightTabel registerClass:[DetaildTableViewCell class] forCellReuseIdentifier:@"right"];
    [self.mainScroll addSubview:_rightTabel];
    [_rightTabel release];
    
}



//创建 data
-(void)createData
{
    
    if (_mainScroll.contentOffset.x >= SCREEN_WIDTH) {
        RequesetManager *requesrt1 = [[RequesetManager alloc] initWithRequestURL:@"http://api2.pianke.me/read/columns_detail" requestModel:POST parameyer:nil];
        requesrt1.parameyerDict = [@{@"deviceid":@"DE849716-5483-4EA5-8F86-F37205610AA9",@"typeid":self.typeID,@"client":@"1",@"sort":@"hot",@"limit":@10,@"version":@"3.0.2",@"auth":@"BsJqECm2x8fhDCMd2GcPplTJxQ8X4N7SF9m1tvD2A2cWP23tFKTx5d4fs",@"start":@0} mutableCopy];
        requesrt1.delegate = self;
        [requesrt1 startRequest];
        _loadin = YES;
    }
    else
    {
        RequesetManager *requesrt = [[RequesetManager alloc] initWithRequestURL:@"http://api2.pianke.me/read/columns_detail" requestModel:POST parameyer:nil];
        requesrt.parameyerDict = [@{@"deviceid":@"6672A5D6-269D-4BAC-9227-3ACBBDFB9FD3",@"typeid":self.typeID,@"client":@"1",@"sort":@"addtime",@"limit":@"10",@"version":@"3.0.2",@"auth":@"XcRoQyXm19KlDCMd2GsfplzJxQ8SrNeOPpzw9UGRicWP23tFEQhVd6PM",@"start":@"0"} mutableCopy];
        requesrt.delegate = self;
        [requesrt startRequest];
    }
    
    
    
}

#pragma mark - RequesrtDelegaet
//JSON 解析
-(void)requestDidFinshWithData:(NSData *)data
{
    if (_mainScroll.contentOffset.x >= SCREEN_WIDTH) {
//        右边的解析
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        NSDictionary *dict1 = dict[@"data"];
        NSArray *arr = dict1[@"list"];
        for (NSDictionary *dict in arr) {
            ReadModel *model = [[ReadModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [self.readRightArr addObject:model];
            [model release];
        }
        [_rightTabel reloadData];
    }
    else{
//        左边的解析
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        NSDictionary *dict1 = dict[@"data"];
        NSArray *arr = dict1[@"list"];
        for (NSDictionary *dict in arr) {
            ReadModel *model = [[ReadModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [self.readLiftArr addObject:model];
            [model release];
        }
        [_liftTabel reloadData];
    }
}
-(void)requestError:(NSError *)error
{
    NSLog(@"error:%@",error);
}


#pragma mark - UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    右边的行数
    if (_mainScroll.contentOffset.x >= SCREEN_WIDTH) {
        return self.readRightArr.count;
    }
//    左边的行数
    return self.readLiftArr.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_mainScroll.contentOffset.x >= SCREEN_WIDTH) {
        DetaildTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"right" forIndexPath:indexPath];
        ReadModel *model = _readRightArr[indexPath.row];
        cell.model = model;
        
        return cell;
    }else {
        DetaildTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"lift" forIndexPath:indexPath];
        ReadModel *model = _readLiftArr[indexPath.row];
        cell.model = model;
        
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 170;
}

-(void)handleChangeFramButtonAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
